if (!window.console) {
	var names = [ "log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count",
			"trace", "profile", "profileEnd" ];

	window.console = {};
	for (var i = 0; i < names.length; ++i)
		window.console[names[i]] = function() {
		}
}
// HTTP POST WITH JSON DATA
jQuery.getJSON = function(url, data, success) {
	return jQuery.ajax({
		type : "GET",
		url : url,
		data : data,
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		// beforeSend:showLoader,
		// complete: hideLoader,
		success : success
	});
};

jQuery.postJSON = function(url, data, success) {
	return jQuery.ajax({
		type : "POST",
		url : url,
		data : JSON.stringify(data),
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		// beforeSend:showLoader,
		// complete: hideLoader,
		success : success
	});
};
jQuery.deleteJSON = function(url, data, success) {
	return jQuery.ajax({
		type : "DELETE",
		url : url,
		data : data,
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		// beforeSend:showLoader,
		// complete: hideLoader,
		success : success
	});
};
jQuery.putJSON = function(url, data, success) {
	return jQuery.ajax({
		type : "PUT",
		url : url,
		data : JSON.stringify(data),
		contentType : "application/json; charset=utf-8",
		dataType : "json",
		// beforeSend:showLoader,
		// complete: hideLoader,
		success : success
	});
};
function setLang(lang) {
	var oLang = getCookie("client-language");
	if (oLang != lang) {
		setCookie1("client-language", lang, "/", getDomainName());
		location.reload();
	}
}
function getDomainName() {
	var names = document.domain.split('.');
	if (names.length > 1) {
		var dname = names[1];
		for (var i = 2; i < names.length; i++) {
			dname += '.' + names[i];
		}
		return dname;
	}
	return document.domain;
}
function setCookie1(name, value, path, domain, expries) {
	var cookieName = name + "=" + escape(value);
	if (expries instanceof Date) {
		cookieName += ";expries=" + expries;
	}
	if (path) {
		cookieName += ";path=" + path;
	}
	if (domain) {
		cookieName += ";domain=" + domain;
	}
	document.cookie = cookieName;
}
function setCookie(name, value, path) {
	if (!path) {
		path = "/";
	}
	var _cookstring = name + '=' + escape(value) + ";path=" + path;
	document.cookie = _cookstring;
}
function getCookie(name) {
	let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); // 正则匹配
	if (arr = document.cookie.match(reg)) {
		return unescape(arr[2]);
	} else {
		return null;
	}
}

/*
 * 用于组装<select>的<option>. 建议的初始示例： <select id="selectId"><option selected="1">selectedVal</option></select>
 * 
 */
function setSelectionOptions(selectId, configGoupCode, selectedVal) {
	if (!selectedVal) {
		selectedVal = $("#" + selectId).val();
	}
	$.get(contextPath + "/rest/common/configs/" + configGoupCode, function(data) {
		var itOpts = "";

		for (x in data) {
			var itm = data[x];
			itOpts += "<option value='" + itm.value;
			if (itm.value == selectedVal) {
				itOpts += "' selected='1";
			}
			itOpts += "'>" + itm.label + "</option>";
		}
		$("#" + selectId).html(itOpts);
	});

}

function encrypt(str, pwd) {
	if (pwd == null || pwd.length <= 0) {
		alert("Please enter a password with which to encrypt the message.");
		return null;
	}
	var prand = "";
	for (var i = 0; i < pwd.length; i++) {
		prand += pwd.charCodeAt(i).toString();
	}
	var sPos = Math.floor(prand.length / 5);
	var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4)
			+ prand.charAt(sPos * 5));
	var incr = Math.ceil(pwd.length / 2);
	var modu = Math.pow(2, 31) - 1;
	if (mult < 2) {
		alert("Algorithm cannot find a suitable hash. Please choose a different password. \nPossible considerations are to choose a more complex or longer password.");
		return null;
	}
	var salt = Math.round(Math.random() * 1000000000) % 100000000;
	prand += salt;
	while (prand.length > 10) {
		prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
	}
	prand = (mult * prand + incr) % modu;
	var enc_chr = "";
	var enc_str = "";
	for (var i = 0; i < str.length; i++) {
		enc_chr = parseInt(str.charCodeAt(i) ^ Math.floor((prand / modu) * 255));
		if (enc_chr < 16) {
			enc_str += "0" + enc_chr.toString(16);
		} else
			enc_str += enc_chr.toString(16);
		prand = (mult * prand + incr) % modu;
	}
	salt = salt.toString(16);
	while (salt.length < 8)
		salt = "0" + salt;
	enc_str += salt;
	return enc_str;
}

function decrypt(str, pwd) {
	if (str == null || str.length < 8) {
		alert("A salt value could not be extracted from the encrypted message because it's length is too short. The message cannot be decrypted.");
		return;
	}
	if (pwd == null || pwd.length <= 0) {
		alert("Please enter a password with which to decrypt the message.");
		return;
	}
	var prand = "";
	for (var i = 0; i < pwd.length; i++) {
		prand += pwd.charCodeAt(i).toString();
	}
	var sPos = Math.floor(prand.length / 5);
	var mult = parseInt(prand.charAt(sPos) + prand.charAt(sPos * 2) + prand.charAt(sPos * 3) + prand.charAt(sPos * 4)
			+ prand.charAt(sPos * 5));
	var incr = Math.round(pwd.length / 2);
	var modu = Math.pow(2, 31) - 1;
	var salt = parseInt(str.substring(str.length - 8, str.length), 16);
	str = str.substring(0, str.length - 8);
	prand += salt;
	while (prand.length > 10) {
		prand = (parseInt(prand.substring(0, 10)) + parseInt(prand.substring(10, prand.length))).toString();
	}
	prand = (mult * prand + incr) % modu;
	var enc_chr = "";
	var enc_str = "";
	for (var i = 0; i < str.length; i += 2) {
		enc_chr = parseInt(parseInt(str.substring(i, i + 2), 16) ^ Math.floor((prand / modu) * 255));
		enc_str += String.fromCharCode(enc_chr);
		prand = (mult * prand + incr) % modu;
	}
	return enc_str;
}
/**
 * 根据key和参数，找到相应的语言标签
 * 
 * @param key
 *            the message key
 * @param params
 *            parameters to replace the placeholder in message. If params is not an array, then treat it as the first
 *            parameter.
 * @return resolved localized message. return the key if message not found.
 */
function resolveLocalizedMessage(key, params) {
	var msg = langMap[key];
	if (msg == undefined) {
		return key;
	}
	if (params) {
		if (params.length) {
			for (var i = 0; i < params.length; i++) {
				msg = msg.replace("{" + i + "}", params[i]);
			}
		} else {
			msg = msg.replace("{0}", params);
		}
	}
	return msg;
}

function transform(key, dictMap) {
    if (dictMap) {
        let _key = key;
        if (key.code) {
            _key = key.code;
        }
        let _label = lookup(_key, dictMap);
        if (_label == _key) {
            if (key.name) {
                _label = key.name;
            }
        }
        return _label;
    }
	return resolveLocalizedMessage(key);
}
/**
 * 把代码映射成字典表中的i18n值，如果代码为空，则返回代码为defaultKey的值，如果连defaultKey也找不到值，则返回代码本身，如果 代码为null或undefined则返回空串。
 * 
 * @param code
 *            要映射的代码
 * @param dictMap
 *            字典数据
 * @param defaultKey
 *            缺省代码，可选参数，缺省为"-"
 */
function lookup(code, dictMap, defaultKey = "-"){
    let _key = code;
    if (!_key) {
        _key = defaultKey;
    }
    if (dictMap) {
        let _val = dictMap[_key];
        if (_val) {
            return _val;
        }
    }
    return _key;
}
